package com.example.im.config.redis;

import com.example.im.constant.Constant;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.FstCodec;
import org.redisson.config.Config;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RedissonConfig {
    @Autowired
    private Environment environment;

    /**
     * 默认过期策略的缓存.
     */
    @SuppressWarnings("serial")
    private static final Map<String, CacheConfig> DEFAULT_CACHE =
            new HashMap<String, CacheConfig>() {
                private static final long MAX_IDLE_TIME = 3600000000000L;
                {
                    put(Constant.CacheExpire.SECONDS_10, new CacheConfig(10000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.SECONDS_30, new CacheConfig(30000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.SECONDS_60, new CacheConfig(60000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.MINUTES_10, new CacheConfig(300000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.MINUTES_30, new CacheConfig(600000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.MINUTES_60, new CacheConfig(1800000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.HOURS_2, new CacheConfig(3600000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.HOURS_4, new CacheConfig(7200000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.HOURS_8, new CacheConfig(14400000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.HOURS_12, new CacheConfig(28800000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.DAY, new CacheConfig(86400000, MAX_IDLE_TIME));
                    put(Constant.CacheExpire.WEEK, new CacheConfig(604800000, MAX_IDLE_TIME));
                }
            };

    @Bean
    public RedissonClient redissonClient(){
        try {
            String json = environment.getProperty("redisson.conf.location");
            InputStream inputStream = new PathMatchingResourcePatternResolver().getResource(json).getInputStream();
            Config config = Config.fromJSON(inputStream);
            config.setCodec(new FstCodec());
            return Redisson.create(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 标准缓存配置.
     * @param redissonClient redisson客户端
     * @throws IOException IO异常
     */
    @Bean
    public CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
        String springCacheManagerConfig = environment.getProperty("spring-cache-starter-location", "");
        if (StringUtils.hasLength(springCacheManagerConfig)) {
            Map<String, ? extends CacheConfig> defined =
                    CacheConfig.fromJSON(springCacheManagerConfig);
            DEFAULT_CACHE.putAll(defined);
        }
        return new RedissonSpringCacheManager(redissonClient);
    }

}
